﻿using System;

namespace Orz.Framework.Foundation
{
    /// <summary>
    /// The class extend System.Math.
    /// </summary>
    public static class MathExtension
    {
        /// <summary>
        /// Get the max value in two values.
        /// </summary>
        /// <param name="d1">The first value.</param>
        /// <param name="d2">The second value.</param>
        /// <returns></returns>
        public static double MaxDouble(double d1,double d2)
        {
            if (double.IsNaN(d1))
            {
                return d2;
            }
            else if (double.IsNaN(d2))
            {
                return d1;
            }

            return Math.Max(d1, d2);
        }

        /// <summary>
        /// Get the max value in two values.
        /// </summary>
        /// <param name="v1">The first value.</param>
        /// <param name="v2">The second value.</param>
        /// <returns></returns>
        public static IComparable Max(IComparable v1, IComparable v2)
        {
            if (v1 == null)
            {
                return v2;
            }
            else if (v2 == null)
            {
                return v1;
            }

            int result = v1.CompareTo(v2);
            if (result >= 0)
            {
                return v1;
            }
            else
            {
                return v2;
            }
        }

        /// <summary>
        /// Get the max value in two values.
        /// </summary>
        /// <param name="v1">The first value.</param>
        /// <param name="v2">The second value.</param>
        /// <returns></returns>
        public static IComparable Min(IComparable v1, IComparable v2)
        {
            if (v1 == null)
            {
                return v1;
            }
            else if (v2 == null)
            {
                return v2;
            }

            int result = v1.CompareTo(v2);
            if (result > 0)
            {
                return v2;
            }
            else
            {
                return v1;
            }
        }
    }
}
